package org.estela.vm;

import java.util.LinkedList;
import java.util.List;

import org.estela.comp.DeclareClassifier;
import org.estela.comp.DeclareFor;
import org.estela.comp.DeclareReference;
import org.estela.comp.InvokeMethod;
import org.estela.comp.ReturnExpression;
import org.estela.comp.StatementVisitor;

public class CodeGenerator implements StatementVisitor {

	private List<Instruction> _frame = new LinkedList<Instruction>(); 
	
	public void visitDeclareClassifier(DeclareClassifier declareClassifier) {
		
	}

	public void visitDeclareReference(DeclareReference declareReference) {
		
	}

	public void visitFor(DeclareFor declareFor) {
		
	}

	public void visitReturn(ReturnExpression returnExpression) {
		
	}


	public void visitInvokeMethod(InvokeMethod invokeMethod) {
		String methodName = invokeMethod.getName();
		Object[] arguments = invokeMethod.getArgumentsValues();
		for(Object obj : arguments) {
			_frame.add(new Instruction(Instruction.LDC, obj));
		}
		_frame.add(new Instruction(Instruction.INVOKE_FUNCTION, methodName));
		_frame.add(new Instruction(Instruction.RETURN));
	}

	public Instruction[] getFrame() {
		return _frame.toArray(new Instruction[_frame.size()]);
	}

}
